package com.alpha.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.Student;
import com.alpha.bom.User;
import com.alpha.dao.CategoryDao;
import com.alpha.dao.ExamResponseDao;

/**
 * Servlet implementation class MyExamServlet
 */
@WebServlet("/MyExamServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"user"}))
public class MyExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(MyExamServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
		
		User user = null; 
		HttpSession session = request.getSession();
		
		String category = "all";
		String type = "all";
		
		if((Student)session.getAttribute("user")!=null){
			user = (Student)session.getAttribute("user");
			ExamResponseDao rDao = new ExamResponseDao();
			
			try {
				((Student)user).setExamResponses(rDao.retrieveExamResponseByStudent(user.getUserId()));
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			
			
			ArrayList<String> categories = new ArrayList<String>();
			CategoryDao cDao = new CategoryDao();
			try {
				categories = cDao.retrieveAllCategories();
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			
			if(categories!=null){
				request.setAttribute("categories", categories);
			}
			
			request.setAttribute("category", category);
			request.setAttribute("type", type);
						
			RequestDispatcher dis=request.getRequestDispatcher("MyExams.jsp");
            dis.forward(request, response);
			
		} else{
			response.sendRedirect("Login.jsp?message=User+not+found.");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" post method started.");
		
		User user = null; 
		HttpSession session = request.getSession();
		
		String category = "all";
		String type = "all";
		
		if((Student)session.getAttribute("user")!=null){
			user = (Student)session.getAttribute("user");
			ExamResponseDao rDao = new ExamResponseDao();
			
			try {
				((Student)user).setExamResponses(rDao.retrieveExamResponseByStudent(user.getUserId()));
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			
			
			ArrayList<String> categories = new ArrayList<String>();
			CategoryDao cDao = new CategoryDao();
			try {
				categories = cDao.retrieveAllCategories();
			} catch (NamingException e) {
				logger.error(e.getMessage());
				return;
			} catch (SQLException e) {
				logger.error(e.getMessage());
				return;
			}
			
			if(categories!=null){
				request.setAttribute("categories", categories);
			}
			
			if(request.getParameter("categories")!=null&&!request.getParameter("categories").equals("")){
				category = request.getParameter("categories");
				request.setAttribute("category", category);
			}
			
			if(request.getParameter("type")!=null&&!request.getParameter("type").equals("")){
				type = request.getParameter("type");
				request.setAttribute("type", type);
			}
			
			
			
			RequestDispatcher dis=request.getRequestDispatcher("MyExams.jsp");
            dis.forward(request, response);
			
		} else{
			response.sendRedirect("Login.jsp?message=User+not+found.");
		}
		
	}

}
